package com.teamtgv.shareshark;

import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.teamtgv.shareshark.R;
import com.teamtgv.shareshark.result.Result;

public class ResultListAdapter extends ArrayAdapter<Result> {

	private static final String LOG_TAG = ResultListAdapter.class.getSimpleName();
	private final List<Result> results;
	private final Context context;

	public ResultListAdapter(Context context, List<Result> results) {
		super(context, R.layout.result_list_item, results);
		this.context = context;
		this.results = results;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View resultView = inflater.inflate(R.layout.result_list_item,
				parent, false);
		Result result = results.get(position);
		
		// Add the fields from the result to the item in the list (found in activity_result_list_item.xml in res/layout)
		
		String itemWhat = result.getSubject();
		String itemBy = ResultViewActivity.RECOMMENDED_BY_PREFIX + result.getRecommender();
		String itemWhere = ResultViewActivity.LOCATION_PREFIX + result.getLocation();
		String itemDate = result.getDateRecommended();
		String itemPlusRating = "+" + String.valueOf(result.getPlusRating());
		String itemMinRating = "-" + String.valueOf(result.getMinRating());
		
		Log.v(LOG_TAG, "=== New Result Item In List ===");
		Log.v(LOG_TAG, "Set subject: " + itemWhat);
		Log.v(LOG_TAG, "set recommender: " + itemBy);
		Log.v(LOG_TAG, "set location: " + itemWhere);
		Log.v(LOG_TAG, "set date: " + itemDate);
		Log.v(LOG_TAG, "set plus rating: " + itemPlusRating);
		Log.v(LOG_TAG, "set min rating: " + itemMinRating);
		Log.v(LOG_TAG, "Image url: -->" + result.getPhotoUrl() + "<--");
		
		((TextView) resultView.findViewById(R.id.itemResultWhat)).setText(itemWhat);
		((TextView) resultView.findViewById(R.id.itemResultRecommender)).setText(itemBy);
		((TextView) resultView.findViewById(R.id.itemResultLocation)).setText(itemWhere);
		((TextView) resultView.findViewById(R.id.itemResultDate)).setText(itemDate);
		((TextView) resultView.findViewById(R.id.itemResultPlusRating)).setText(itemPlusRating);
		((TextView) resultView.findViewById(R.id.itemResultMinRating)).setText(itemMinRating);
		
		if (result.getRecommender().equals(UserData.UNKNOWN_USER)) {
			((TextView) resultView.findViewById(R.id.itemResultRecommender)).setHeight(0); // hide field
		}
		
		return resultView;
	}
}
